﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;

namespace ASmile.Extern.Lib.Rsp
{
    public abstract class HttpResponseAsync : HttpResponseDefault
    {
        static HttpResponseAsync()
        {
            //AsyncData = new Dictionary<string, Dictionary<string, string>>();
        }

        public HttpResponseAsync()
        {
            AsyncData[Type] = new ApiResult { code = 0, ExtendData = new DictExtend() };
        }
        /// <summary>
        /// 每次http请求最多等待时间(秒)，默认10秒
        /// </summary>
        protected int MaxWaitSec { get; set; } = 10;
        /// <summary>
        /// 每次等待结果的时间(毫秒)，默认10毫秒
        /// </summary>
        protected int OnceInterval { set; get; } = 10;

        static Dictionary<string, ApiResult> AsyncData { get; } = new Dictionary<string, ApiResult>();

        public static void SetData(string type, Dictionary<string, string> data)
        {
            var ret = AsyncData[type];
            ret.ExtendData.SetObject(data);
            //foreach (var item in data)
            //{
            //    ret.data[item.Key] = item.Value;
            //}
            ret.code = 0;
        }

        public static void SetData(string type, string key, string val)
        {
            var ret = AsyncData[type];
            ret.ExtendData.SetValue(key, val);

            //var ret = AsyncData[type];
            //ret.data[key] = val;
            //ret.success = true;
        }

        public void SetData(Dictionary<string, string> data)
        {
            var ret = AsyncData[Type];
            ret.ExtendData.SetObject(data);
            ret.code = 0;

            //foreach (var item in data)
            //{
            //    ret.data[item.Key] = item.Value;
            //}
            //ret.success = true;
        }

        public void SetData(string key, string val)
        {
            var ret = AsyncData[Type];
            ret.ExtendData.SetValue(key,val);
            ret.code = 0;

            //ret.data[key] = val;
            //ret.success = true;
        }

        ApiResult CurrResult { get { return AsyncData[Type]; } }

        protected override ApiResult GetResponseObject(Dictionary<string, string> jsonObj)
        {
            throw new NotImplementedException();
        }

        protected abstract void ReceiveJsonData(Dictionary<string, string> bodyData);

        protected override void ReceiveJsonData(HttpListenerContext context, Dictionary<string, string> bodyData)
        {
            //if (!IsRun)
            //{
            //    IsRun = true;
            //    ReceiveJsonData(bodyData);
            //}

            ReceiveJsonData(bodyData);

            int intervalTotal = 0; //间隔毫秒
            while (intervalTotal < MaxWaitSec * 1000)
            {
                Thread.Sleep(OnceInterval);
                intervalTotal += OnceInterval;
                if (CurrResult.success)
                {
                    ResponseObject(context, CurrResult);
                    //CurrResult.code = false;
                    CurrResult.code = 1;
                    return;
                }
            }

            ResponseObject(context, CurrResult);
        }
    }
}